package org.example.order.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 小杰_cc
 * @version 1.0
 */
@Configuration
public class MyMqConfig{

    @Bean
    public TopicExchange orderEventExchange() {
        return ExchangeBuilder.topicExchange("order-event-exchange").durable(true).build();
    }

    // 普通队列
    @Bean
    public Queue orderReleaseQueue() {
        return QueueBuilder.durable("order.release.order.queue").build();
    }

    // 普通队列
    @Bean
    public Queue stockReleaseQueue(){
        return QueueBuilder.durable("stock.release.stock.queue").build();
    }


    // 延迟队列
    @Bean
    public Queue orderDelayQueue(){
        return QueueBuilder.durable("order.delay.queue")
                .ttl(900000)    // 15分钟
                .deadLetterRoutingKey("order.release.order")
                .deadLetterExchange("order-event-exchange")
                .build();
    }

    // 绑定关系
    @Bean
    public Binding orderCreateBinding(){
        return BindingBuilder.bind(orderDelayQueue()).to(orderEventExchange()).with("order.create.order");
    }

    // 绑定关系
    @Bean
    public Binding orderReleaseBinding(){
        return BindingBuilder.bind(orderReleaseQueue()).to(orderEventExchange()).with("order.release.order");
    }

    // 绑定关系
    @Bean
    public Binding orderReleaseOtherBinding(){
        return BindingBuilder.bind(stockReleaseQueue()).to(orderEventExchange()).with("order.release.other.#");
    }
}
